package com.ukao.cashregister.ui.clothinQuery;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bm.library.PhotoView;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.utils.ImageUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 单张图片显示Fragment
 */
public class PhotoDetailFragment extends BaseFragment implements RequestListener {


    @BindView(R.id.image)
    PhotoView image;
    @BindView(R.id.pb_load_local)
    ProgressBar progressBar;
    private String mImageUrl;
    public boolean mHasLoadedOnce;
    private View rootview;


    private Unbinder unbinder;


    public static PhotoDetailFragment newInstance(String imageUrl) {
        final PhotoDetailFragment f = new PhotoDetailFragment();
        final Bundle args = new Bundle();
        args.putString("url", imageUrl);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImageUrl = getArguments() != null ? getArguments().getString("url")
                : null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_image_detail, container, false);
        unbinder = ButterKnife.bind(this, rootview);
        image.enable();
        image.setScaleType(ImageView.ScaleType.CENTER);
        return rootview;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if ( mHasLoadedOnce) return;

        ImageUtils.loadImage(_mActivity, mImageUrl, image, this);

    }

    @Override
    protected void initView() {

    }


    @Override
    public boolean onException(Exception e, Object o, Target target, boolean b) {
        if (progressBar != null)
            progressBar.setVisibility(View.GONE);


        return false;
    }

    @Override/*加载成功*/
    public boolean onResourceReady(Object o, Object o2, Target target, boolean b, boolean b1) {
        mHasLoadedOnce = true;
        if (progressBar != null)
            progressBar.setVisibility(View.GONE);
        return false;
    }


}
